<?php
// Module Body

function  tux_domain_boot(&$data) {
	$SQL = 'SELECT id, path, host, priority, ip, domain, type FROM domains WHERE type!="subdomain"';
	$res = mysql_query($SQL);
	if (mysql_num_rows($res)) {
		for($i=0; $row=mysql_fetch_array($res); $i++) {
			$row['ip'] = explode('|', $row['ip']);
			$row['norestart'] = true;
			tux_stop();
			tux_domain_create($row);
			tux_start();
		}
	}
}

function  tux_domain_create(&$data) {
	global $cfg;

	if (!$data['norestart']) { tux_stop(); }
	
	switch ($data['type']) {
		case 'pointer':
			$docroot = $data['path'];
			break;
		case 'domain':
			$docroot = $data['path']."/".$data['host']."/".$cfg['webname'];
			if ($data['priority']) {
				foreach($data['ip'] as $IP) {
					$tdir = $cfg['tuxdir'].'/'.$IP;
					if (is_dir($tdir)) {
						exec('umount '.$tdir);
					} else {
						mkdir($tdir);
					}
					exec('mount --bind '.$docroot.' '.$tdir);
				}
			}
			break;
		case 'subdomain':
			return;
			break;
	}
	
	$tdir = $cfg['tuxdir'].'/'.$data['domain'];
	if (is_dir($tdir)) {
		exec('umount '.$tdir);
	} else {
		mkdir($tdir);
	}
	exec('mount --bind '.$docroot.' '.$tdir);

	if (!$data['norestart']) {
		tux_start();
	}
}

function tux_domain_update(&$data) {
	global $cfg;

	tux_stop();

	switch ($data['type']) {
		case 'pointer':
			$docroot = $data['path'];
			break;
		case 'domain':
			$docroot = $data['path']."/".$data['host']."/".$cfg['webname'];
			if ($data['priority']) {
				foreach($data['ip'] as $IP) {
					$tdir = $cfg['tuxdir'].'/'.$IP;
					if (is_dir($tdir)) {
						exec('umount '.$tdir);
					} else {
						mkdir($tdir);
					}
					exec('mount --bind '.$docroot.' '.$tdir);
				}
			}
			break;
		case 'subdomain':
			return;
			break;
	}
	
	$tdir = $cfg['tuxdir'].'/'.$data['domain'];
	if (is_dir($tdir)) {
		exec('umount '.$tdir);
	} else {
		mkdir($tdir);
	}
	exec('mount --bind '.$docroot.' '.$tdir);

	tux_start();
}

function tux_domain_suspend(&$data) {
	global $cfg;

	tux_stop();

	$docroot = $cfg['basedir'].'/suspended';

	if ($data['priority'] && $data['type'] == 'domain') {
		foreach($data['ip'] as $IP) {
			$tdir = $cfg['tuxdir'].'/'.$IP;
			if (is_dir($tdir)) {
				exec('umount '.$tdir);
			} else {
				mkdir($tdir);
			}
			exec('mount --bind '.$docroot.' '.$tdir);
		}
	}
	
	$tdir = $cfg['tuxdir'].'/'.$data['domain'];
	if (is_dir($tdir)) {
		exec('umount '.$tdir);
	} else {
		mkdir($tdir);
	}
	exec('mount --bind '.$docroot.' '.$tdir);

	tux_start();
}

function tux_domain_unsuspend(&$data) {
	global $cfg;

	tux_stop();

	switch ($data['type']) {
		case 'pointer':
			$docroot = $data['path'];
			break;
		case 'domain':
			$docroot = $data['path']."/".$data['host']."/".$cfg['webname'];
			if ($data['priority']) {
				foreach($data['ip'] as $IP) {
					$tdir = $cfg['tuxdir'].'/'.$IP;
					if (is_dir($tdir)) {
						exec('umount '.$tdir);
					} else {
						mkdir($tdir);
					}
					exec('mount --bind '.$docroot.' '.$tdir);
				}
			}
			break;
		case 'subdomain':
			return;
			break;
	}
	
	$tdir = $cfg['tuxdir'].'/'.$data['domain'];
	if (is_dir($tdir)) {
		exec('umount '.$tdir);
	} else {
		mkdir($tdir);
	}
	exec('mount --bind '.$docroot.' '.$tdir);

	tux_start();
}

function tux_domain_remove(&$data) {
	global $cfg;

	tux_stop();

	if ($data['priority'] && $data['type'] == 'domain') {
		foreach($data['ip'] as $IP) {
			$tdir = $cfg['tuxdir'].'/'.$IP;
			if (is_dir($tdir)) {
				exec('umount '.$tdir);
				rmdir($tdir);
			}
		}
	}
	
	$tdir = $cfg['tuxdir'].'/'.$data['domain'];
	if (is_dir($tdir)) {
		exec('umount '.$tdir);
		rmdir($tdir);
	}

	$SQL = 'SELECT path, host, priority, ip, domain FROM domains WHERE owner = "'.$data['id'].'"';
	$res = mysql_query($SQL);
	if (mysql_num_rows($res)) {
		for($i=0; $row=mysql_fetch_array($res); $i++) {
			$tdir = $cfg['tuxdir'].'/'.$row['domain'];
			if (is_dir($tdir)) {
				exec('umount '.$tdir);
				rmdir($tdir);
			}
		}
	}

	tux_start();
}

function tux_monitor(&$data) {
	global $cfg;
	exec('ps aux | grep "TUX manager"', $result);
	if (preg_match('/[0-9]+/', $result[0], $match)) {
		$pid = $match[0];
		if (posix_kill($pid,0)) {
			$data['tux'] = 'up';
		} else {
			$data['tux'] = 'down';
		}
	} else {
		$data['tux'] = 'down';	
	}
	if ($data['tux'] == 'down') {
		if ($cfg['service_auto_start'] && $cfg['autostart']['tux']) {
			webcp_log(2,0,"monitor","service down: tux - attempting to start",0,$echo);
			tux_start();
			sleep(2);
			exec('ps aux | grep "TUX manager"', $result);
			if (preg_match('/[0-9]+/', $result[0], $match)) {
				$pid = $match[0];
				if (posix_kill($pid,0)) {
					$data['tux'] = 'up';
				} else {
					$data['tux'] = 'down';
				}
			} else {
				$data['tux'] = 'down';	
			}
		}
	}
	if ($data['tux'] == 'down') {
		webcp_log(1,0,"monitor","service down: tux",0,$echo);
	}
	return true;
}

function tux_add_config(&$cfg) {
	$cfg['tuxdir'] = '/var/www/html';
	$cfg['spid']['tux'] = '/var/run/tix.pid';
	$cfg['init']['tux'] = '/etc/init.d/tux';
	$cfg['autostart']['tux'] = true;
}

function tux_stop() {
	global $cfg;
	exec($cfg['init']['tux'].' stop');
}

function tux_start() {
	global $cfg;
	exec($cfg['init']['tux'].' start');
}

function tux_restart() {
	global $cfg;
	exec($cfg['init']['tux'].' restart');
}

function tux_add_lang(&$data) {
	global $T;
	switch($data['lang']) {
		default:
			$T['tux service'] = 'RedHat Content Accelerator';
			break;
	}
}
?>
